//
//  CZQGoodListVC.m
//  coachmanage
//
//  Created by zhiqiang chen on 2018/4/11.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQGoodListVC.h"
#import "VenueVipTableViewCell.h"
#import "VenueVipModel.h"

@interface CZQGoodListVC ()<UITableViewDelegate,UITableViewDataSource>
/**
 //数据源
 */
@property(nonatomic,strong)NSMutableArray *dataArr;

/**
 师傅关注数据源
 */
@property(nonatomic,strong)NSMutableArray *attendArr;


/**
 tableView
 */
@property(nonatomic,weak)UITableView *tableView;

@property(nonatomic,strong)NoDataView *noDataView;

@property(nonatomic,assign)NSInteger startIndex;

@property(nonatomic,assign)NSInteger pageSize;
@end

@implementation CZQGoodListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BackGroundColor;
    
    [self tableView];
    
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    
    
    [self getGoodList];
    
}


#pragma mark 下拉
-(void)loadStartData{
    
    self.startIndex = 1;
    
    self.pageSize = 20;
    
    [self getGoodList];
    
    
    
    
}

#pragma mark 上拉
-(void)loadMoreData{
    
    self.startIndex = self.startIndex + 1;
    
    [self getGoodList];
}

#pragma mark - 获取师傅关注列表判断自己是否关注
-(void)getMasterAttentList
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    NSString *userId = [[NSUserDefaults standardUserDefaults] valueForKey:@"UserID"];
    
    [params setValue:userId  forKey:@"MemberID"];
    [params setValue:[[NSUserDefaults standardUserDefaults]objectForKey:@"MasterType" ]  forKey:@"UserType"];
    
    
    
    
    [HTTPTool GET:Get_Activity_ActivityFollowList parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              NSLog(@"%@=%@",responseObject,params);
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  if (resultArray.count == 0) {
                      return ;
                  }
                  
                  
                  
                  [self.attendArr addObjectsFromArray:resultArray];
                  
                  [self.tableView reloadData];
                  
                  
              }
              
          }
     
          failure:^(NSError *error){
              
              [self.tableView.mj_header endRefreshing];
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}



#pragma mark - 获取点赞列表
-(void)getGoodList
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    NSString *userId = [[NSUserDefaults standardUserDefaults] valueForKey:@"UserID"];
    
    NSString *url ;
    
    if ([self.title isEqualToString:@"加油"]) {
        
        [params setValue:self.ID forKey:@"ActivityID"];
        [params setValue :[NSString stringWithFormat:@"%ld",self.startIndex] forKey:@"page"];
        [params setValue :[NSString stringWithFormat:@"%ld",self.pageSize] forKey:@"rows"];
        
        url = Get_Activity_GetActivityVoteList;
        
    }else
    {
        
        [params setValue:userId forKey:@"MemberID"];
        [params setValue :@"1" forKey:@"UserType"];
        
        url = Get_Activity_ActivityFollowList;
        
    }
    
    
    
    
    [HTTPTool GET:url parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              [self.tableView.mj_header endRefreshing];
              
              NSLog(@"%@=%@",responseObject,params);
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  
                  if (resultArray.count > 0) {
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshing];
                  }
                  else{
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshingWithNoMoreData];
                  }
                  
                  if (self.startIndex == 1) {
                      [self.dataArr removeAllObjects];
                  }
                  
                  
                  
                  
                  
                  for (NSDictionary *resultDic in resultArray) {
                      
                      VenueVipModel *venueVipModel = [VenueVipModel mj_objectWithKeyValues:resultDic];
                      
                      [self.dataArr addObject:venueVipModel];
                      
                  }
                  self.noDataView = [NoDataView shareInstance];
                  
                  [self.noDataView hideInView:self.view];
                  
                  if (self.dataArr.count == 0) {
                      
                      self.noDataView = [NoDataView shareInstance];
                      
                      [self.noDataView showInView:self.view];
                  }
                  
                  [MBProgressHUD hideHUDForView:self.view];
                  
                  [self getMasterAttentList];
                  
                  [self.tableView reloadData];
              }
              
          }
     
          failure:^(NSError *error){
              
              [self.tableView.mj_header endRefreshing];
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}

#pragma mark -- 懒加载

-(NSMutableArray *)attendArr
{
    if (!_attendArr) {
        
        _attendArr = [NSMutableArray array];
        
    }
    
    return _attendArr;
}

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
        
    }
    
    return _dataArr;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        tableView.delegate=self;
        
        tableView.dataSource=self;
        
        tableView.backgroundColor = [UIColor clearColor];
        
        tableView.estimatedRowHeight = 150;
        
        [self.view addSubview:tableView];
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self.view);
            
        }];
        
        if ([self.title isEqualToString:@"加油"]) {
            MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
            footer.automaticallyHidden = YES;
            tableView.mj_footer = footer;

        }
        
        
        
        MyTableHead *mj_head = [MyTableHead headerWithRefreshingTarget:self refreshingAction:@selector(loadStartData)];
        tableView.mj_header = mj_head;
        [tableView.mj_header beginRefreshing];
        
        
        [tableView registerClass:[VenueVipTableViewCell class] forCellReuseIdentifier:@"VenueVipTableViewCell"];
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource

//行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//列数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    VenueVipTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VenueVipTableViewCell"];
    
    
    
    VenueVipModel *venueVipModel = [self.dataArr objectAtIndex:indexPath.row];
    //0 是男
    cell.sexBtn.hidden = YES;
    
    cell.nickNameLabel.text = venueVipModel.Name ;
    
    cell.phoneLabel.hidden = YES;
    
    cell.ageBtn.hidden = YES;

    [cell.rightImageView setImage:nil];
    
    cell.rightImageView.tag = indexPath.row + 100;
    
    [cell.rightImageView addTarget:self action:@selector(attend:) ];
    
    [cell.rightImageView setTitle:@"关注"];
    
    for (NSDictionary *dic in self.attendArr) {
        
        
        
        if ([dic[@"MemberID"] integerValue] == [venueVipModel.MemberID integerValue]) {
            
            [cell.rightImageView setTitle:@"取消关注"];
            
            break;
        }
        
    }

    
    
    
    
    cell.rightImageView.titleLabel.font = MidBigFont;
    [cell.rightImageView setTitleColor:GrakColor];
    cell.rightImageView.layer.borderWidth = 1;
    cell.rightImageView.layer.borderColor = CSGrayColor.CGColor;
    [cell.rightImageView setContentEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[Helper imageUrlStringToChinese:venueVipModel.Pic imageType:nil]] placeholderImage:PlaceHold];
    
    return cell  ;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当离开某行时，让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - 关注
-(void)attend:(UIButton *)sender
{
    sender.selected = !sender.selected;
    
    NSString *url;
    
    NSString *tip;
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    VenueVipModel *venueVipModel = [self.dataArr objectAtIndex:sender.tag - 100];
    
    if ([sender.title isEqualToString:@"关注"]) {
        //关注
        url = Post_Activity_FollowOperate;
        
        tip = @"关注成功";
        
    }else
    {
        //取消关注
        url = Post_Activity_DelFollow;
        
        tip = @"取消关注成功";
        
    }
    
    [params setValue:[[NSUserDefaults standardUserDefaults] valueForKey:@"UserID"] forKey:@"MemberID"];
    
    [params setValue:[[NSUserDefaults standardUserDefaults] valueForKey:@"MasterType"] forKey:@"UserType"];
    
    if ([self.title isEqualToString:@"加油"]) {
        [params setValue:venueVipModel.MemberID forKey:@"ToMemberID"];
        [params setValue:venueVipModel.UserType forKey:@"ToUserType"];
    }else
    {
        [params setValue:venueVipModel.ToMemberID forKey:@"ToMemberID"];
        [params setValue:venueVipModel.ToUserType forKey:@"ToUserType"];
    }
    
    
    
    
    
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    
    
    [HTTPTool POSTRaw:url parameters:params
     
              success:^(id responseObject){
                  
                  [MBProgressHUD hideHUDForView:self.view];
                  
                  
                  if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                      
                      
                      [MBProgressHUD showText:tip];
                      
                      [MBProgressHUD hideHUDForView:self.view];
                      
                      //刷新数据
                      [self getGoodList];
                  }
                  
              }
     
              failure:^(NSError *error){
                  
                  [MBProgressHUD hideHUDForView:self.view];
              }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
